package HLogger

import (
	"frame/utils/PropertiesUtils"
	"strings"
)

const DEFAULT_layout = "[%T][%P][%i{14}][%d{20060102 150405.000}][%f{35}]: %m%n"

//const DEFAULT_layout = " %m%n"

type HLogger struct {
	Name string
	Appenders []HLoggerAppender
}

func GetHLogger(name string)(HLogger){
	var h = HLogger{Name :name}

	var logroot,_ = PropertiesUtils.GetConfig("logger","logpath","./logs")
	var subpath,_ = PropertiesUtils.GetConfig("logger","subpath","txn")
	var rootname,_ = PropertiesUtils.GetConfig("logger","rootname","ALL-SERVER-LOG")
	var layout,_ = PropertiesUtils.GetConfig("logger","layout",DEFAULT_layout)
	h.Appenders = append(h.Appenders,HLoggerAppender{
		appendtype:APPENDTYPE_CONSOLE,
		layout:layout})
	h.Appenders = append(h.Appenders,HLoggerAppender{
		appendtype:APPENDTYPE_ALLFILE,
		rootpath:logroot,
		rootname:rootname,
		layout:layout,
		splitbydate:true})
	h.Appenders = append(h.Appenders,HLoggerAppender{
		appendtype:APPENDTYPE_TXNFILE,
		rootpath:logroot,
		subpath:subpath,
		name:name,
		layout:layout,
		splitbydate:true})

	return h
}



func needLog(loggerlevel string)(bool){
	var currlevel,_ = PropertiesUtils.GetConfig("logger","loglevel","debug")
	currlevel = strings.ToLower(currlevel)
	if(loggerlevel=="error"){
		return true
	}
	if(loggerlevel=="debug" && currlevel=="debug"){
		return true
	}
	if(loggerlevel=="info" && (currlevel=="debug"|| currlevel=="info")){
		return true
	}
	return false
}

func (logger HLogger)Info(i ... string){
	if(needLog("info")){
		for _,hla :=range logger.Appenders {
			var m = make(map[string]string)
			m["loglevel"]="INF"
			var txnContext = TxnContext{"0",m}
			for _,v :=range i{
				hla.Append(txnContext,v)
			}
		}
	}
}
func (logger HLogger)Debug(i ... string){
	if(needLog("debug")){
		for _,hla :=range logger.Appenders {
			var m = make(map[string]string)
			m["loglevel"]="DEB"
			var txnContext = TxnContext{"0",m}
			for _,v :=range i{
				hla.Append(txnContext,v)
			}
		}
	}
}
func (logger HLogger)Error(i ... string){
	if(needLog("error")){
		for _,hla :=range logger.Appenders {
			var m = make(map[string]string)
			m["loglevel"]="ERR"
			var txnContext = TxnContext{"0",m}
			for _,v :=range i{
				hla.Append(txnContext,v)
			}
		}
	}
}
func (logger HLogger)InfoWithTxnContext(txnContext TxnContext,i ... string){
	if(needLog("info")){
		for _,hla :=range logger.Appenders {
			if(txnContext.Expandinfo==nil){
				txnContext.Expandinfo=make(map[string]string)
			}
			txnContext.Expandinfo["loglevel"]="INF"
			for _,v :=range i{
				hla.Append(txnContext,v)
			}
		}
	}
}

func (logger HLogger)DebugWithTxnContext(txnContext TxnContext,i ... string){
	if(needLog("debug")){
		for _,hla :=range logger.Appenders {
			if(txnContext.Expandinfo==nil){
				txnContext.Expandinfo=make(map[string]string)
			}
			txnContext.Expandinfo["loglevel"]="DEB"
			for _,v :=range i{
				hla.Append(txnContext,v)
			}
		}
	}
}
func (logger HLogger)ErrorWithTxnContext(txnContext TxnContext,i ... string){
	if(needLog("error")){
		for _,hla :=range logger.Appenders {
			if(txnContext.Expandinfo==nil){
				txnContext.Expandinfo=make(map[string]string)
			}
			txnContext.Expandinfo["loglevel"]="ERR"
			for _,v :=range i{
				hla.Append(txnContext,v)
			}
		}
	}
}